/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.dao.mock;

import org.activequant.core.domainmodel.account.Account;
import org.activequant.dao.IAccountDao;
import org.activequant.util.exceptions.DaoException;

/**
 * @TODO desc<br>
 * <br>
 * <b>History:</b><br>
 *  - [03.06.2007] Created (Erik Nijkamp)<br>
 *  - [27.10.2007] Added MockDaoSupportBase (Erik Nijkamp)<br>
 *  - [31.10.2007] Adding returning of a new account in case none was found. marking methods as overriders (Ulrich Staudinger)<br>
 *
 *  @author Erik Nijkamp
 *  @author Ulrich Staudinger
 */
public class MockAccountDao extends MockDaoSupportBase<Account> implements IAccountDao {
	
	public MockAccountDao() {
		super(Account.class);
	}
	
	Long getId(Account t) {
		return t.getId();
	}
	
	void setId(Account t, Long id) {
		t.setId(id);
	}

	public Account findByHolder(String name) throws DaoException {
		for(Account account : database.values()) {
			if(account.getHolder().equals(name)) {
				return account;
			}
		}
		// empty account
		Account account = new Account();
		account.setHolder(name);
		this.update(account);
		return account;
	}

	@Override
	public Account find(long id) throws DaoException {
		for(Account account : database.values()) {
			if(account.getId() == id) {
				return account;
			}
		}
		// 
		Account account = new Account();
		account.setId(id);
		this.update(account);
		return account;
	}
	
	
	@Override
	protected boolean compareByExample(Account model, Account sample) {
		if(model.getHolder() != null) {
			if(model.getHolder().equals(sample.getHolder()) == false) {
				return false;
			}
		}
		
		return true;
	}
}
